import {Body, Controller, ForbiddenException, Get, Post, Query, UseGuards} from '@nestjs/common';
import {ProductService} from './product.service';
import {newproduct} from './dto/newproduct.dto';
import {processinfo} from './dto/processinfo.dto';
import {transport} from './dto/transport.dto';
import {collectinfo} from './dto/clooectinfo.dto';
import {UserService} from 'src/user/user.service';
import {AuthGuard} from "@nestjs/passport";

@UseGuards(AuthGuard('jwt'))
@Controller('product')
export class ProductController {
    constructor(private ProductService: ProductService,
                private UserService: UserService) {
    }

    //获得所有产品
    @Get('/all')
    async getAll() {
        const proList = await this.ProductService.getAllProduct();
        return {
            statusCode: 200,
            res: {
                proList
            }
        }
    }

    @Get('/certificate')
    async getCertificate(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const certificateId = await this.ProductService.getCertificate(proAdd)
        console.log(certificateId)
        return {
            statusCode: 200,
            res: {
                certificateId
            }
        }
    }

    //获得某一用户所有产品
    @Get('/ones-product')
    async getProFromUser(@Query('userAdd') userAdd: string) {
        const user = await this.UserService.findOneByAdd(userAdd);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        const proList = await this.ProductService.getProFromUser(userAdd);
        return {
            statusCode: 200,
            res: {
                proList
            }
        }
    }

    //获得某一产品基本信息
    @Get('/pro-basic-info')
    async getProBasic(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const proBasic = await this.ProductService.getProBasic(proAdd);
        return {
            statusCode: 200,
            res: {
                proBasic
            }
        }
    }

    //获得某一产品采集信息
    @Get('/pro-collect-info')
    async getProCollect(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const temp = await this.ProductService.getProCollect(proAdd);
        const {'0': time, '1': collectAddress, '2': operator, '3': imageId, '4': message} = temp
        return {
            statusCode: 200,
            proCollectInfo: {
                time, collectAddress, operator, imageId, message
            }
        }

    }

    //获得某一产品生长过程信息
    @Get('/pro-process')
    async getProProcess(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const proProcess: Array<any> = await this.ProductService.getProProcess(proAdd);
        const processList = [];
        proProcess.forEach(s => {
            const temp: any = {};
            temp.time = s.at(0);
            temp.temperature = s.at(1);
            temp.weather = s.at(2);
            temp.operator = s.at(3);
            temp.imageId = s.at(4);
            temp.message = s.at(5);
            processList.push(temp)
        })
        return {
            statusCode: 200,
            res: {
                processList
            }
        }
    }

    //获得某一产品物流信息
    @Get('/pro-transport')
    async getProTransport(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const proTransport: Array<any> = await this.ProductService.getProTransport(proAdd);
        // console.log("00000",proTransport[0]);
        // const aa=proTransport.flat()
        const transportList = [];
        proTransport.forEach(s => {
            const temp: any = {};
            temp.time = s.at(0);
            temp.transportAddress = s.at(1);
            temp.operator = s.at(2);
            temp.carLicense = s.at(3);
            transportList.push(temp)
        })
        // console.log("----------",transportList)
        return {
            statusCode: 200,
            res: {
                transportList
            }
        }
    }

    //获得某一产品是否删除
    @Get('/pro-deleted')
    async getIsDeleted(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const isDeleted = await this.ProductService.getIsDeleted(proAdd);
        return {
            statusCode: 200,
            res: {
                isDeleted
            }
        }
    }

    //获得某一产品销售地址
    @Get('/pro-sell-address')
    async getSellAddress(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const sellAddress = await this.ProductService.getSellAddress(proAdd);
        return {
            statusCode: 200,
            res: {
                sellAddress
            }
        }
    }

    //获得某一产品审批状态
    @Get('/approved')
    async getIfApprove(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const {approved, appMessage, approver} = await this.ProductService.getIfApprove(proAdd);
        return {
            statusCode: 200,
            res: {
                approved,
                message:appMessage,
                approver
            }
        }
    }

    //获得产品审批意见
    @Get('/approve-message')
    async getApproveMessage(@Query('proAdd') proAdd: string) {
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const approveMessage = await this.ProductService.getAppMessage(proAdd);
        return {
            statusCode: 200,
            res: {
                approveMessage
            }
        }
    }

    //创建新产品
    @Post('/new-product')
    async createPro(@Body() newProduct: newproduct) {
        const user = await this.UserService.findOne(newProduct.applicant);
        if (!user) {
            throw new ForbiddenException('非法用户！');
        } else if (user.address !== newProduct.owner) {
            throw new ForbiddenException('非法用户地址,请检查');
        }
        const res = await this.ProductService.createPro(newProduct);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //申请产品审批
    @Post('/apply')
    async applyApprove(@Body() body: { proAdd: string, certificate: string, price: string }) {
        const {proAdd, certificate, price} = body;
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        console.log(proAdd, certificate, price)
        const res = await this.ProductService.applyApprove(proAdd, certificate, price);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //重新申请审批
    @Post('/apply-again')
    async applyAgain(@Body() body: { proAdd: string, certificate: string, price: string }) {
        const {proAdd, certificate, price} = body;
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const res = await this.ProductService.applyAgain(proAdd, certificate, price);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //管理员审批
    @Post('/set-if-approve')
    async setIsApprove(@Body() body: { proAdd: string, approver: string, isApprove: number, appMessage: string }) {
        const {proAdd, approver, isApprove, appMessage} = body;
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const auditor = await this.UserService.findOne(approver);
        if (!auditor || auditor.range == 2) {
            throw new ForbiddenException('无权限用户');
        }
        const res = await this.ProductService.setIfApprove(proAdd, approver, isApprove, appMessage);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //设置产品是否下架
    @Post('/set-deleted')
    async setIsDeleted(@Body() body: { proAdd: string, isDeleted: boolean }) {
        let {proAdd, isDeleted} = body;
        // @ts-ignore
        if (isDeleted === 'false')
            isDeleted = false
        // @ts-ignore
        else if (isDeleted === 'true') {
            isDeleted = true
        }
        console.log(proAdd, isDeleted)
        const res = await this.ProductService.setIsDeleted(proAdd, isDeleted);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    @Post('/set-sell-address')
    async setSellAddress(@Body() body: { proAdd: string, sellAdd: string }) {
        const {proAdd, sellAdd} = body;
        const res = await this.ProductService.setSellAddress(proAdd, sellAdd);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //设置采集信息
    @Post('/set-collect-info')
    async setCollectInfo(@Body() body: { proAdd: string, collectInfo: collectinfo }) {
        const {proAdd, collectInfo} = body;
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const res = await this.ProductService.setProCollect(proAdd, collectInfo);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }

    //添加物流信息
    @Post('/push-transport')
    async pushTransport(@Body() body: { proAdd: string, trans: transport }) {
        const {proAdd, trans} = body;
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const res = await this.ProductService.pushTransportToPro(proAdd, trans);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }


    //添加作物生长过程信息
    @Post('/push-process')
    async pushProcess(@Body() body: { proAdd: string, process: processinfo }) {
        const {proAdd, process} = body;
        console.log(process);
        const product = await this.ProductService.findone(proAdd);
        if (!product) {
            throw new ForbiddenException('查无此产品，请检查输入');
        }
        const res = await this.ProductService.pushProcessToPro(proAdd, process);
        return {
            statusCode: 201,
            res: {
                success: "success",
                res
            }
        }
    }
}
